package ar.edu.unicen.exa.isistan.archassis.view.classes;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;

import ar.edu.unicen.exa.isistan.archassis.ia.api.db.JavaClass;
import ar.edu.unicen.exa.isistan.archassis.view.ListComposite;
import ar.edu.unicen.exa.isistan.archassis.view.classes.classitem.ClassComposite;

import org.eclipse.wb.swt.SWTResourceManager;

public class ClassesComposite extends ListComposite {
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public ClassesComposite(Composite parent) {
		super(parent, SWT.NONE);
		child.setBackground(SWTResourceManager.getColor(SWT.COLOR_LIST_BACKGROUND));
		child.setForeground(SWTResourceManager.getColor(SWT.COLOR_LIST_BACKGROUND));
	    
	}

	private Map<String, ClassComposite> classList = new HashMap<String, ClassComposite>();
	
	public List<JavaClass> getClassesList() {
		List<JavaClass> classListResult = new ArrayList<JavaClass>();
		for (ClassComposite classComposite: classList.values()) {
			if (classComposite.isChecked()) {
				classListResult.add(classComposite.getJavaClass());
			}
			
		}
		return classListResult;
	}
	
	public void change(List<JavaClass> classesList) {
		System.out.println("eh");
		disposeChildrens();
		classList.clear();
		
		if (classesList == null) {
			noChildrens();
			return;
		}
		
		child.setLayout(new FormLayout());
		
		Composite lastItem = null;
		
		for (JavaClass javaClass : classesList) {
			
			ClassComposite child2 = new ClassComposite(child, SWT.NONE);
			child2.setJavaClass(javaClass);
			
			FormData fd_lblNewLabel = new FormData();
		    fd_lblNewLabel.right = new FormAttachment(100, -5);
		    
		    fd_lblNewLabel.left = new FormAttachment(0, 5);
			if (lastItem == null) {
				fd_lblNewLabel.top = new FormAttachment(0, 10);
			} else {
				fd_lblNewLabel.top = new FormAttachment(lastItem, 15);
			}
			lastItem = child2;
			child2.setLayoutData(fd_lblNewLabel);
			classList.put(javaClass.getNombre(), child2);
		}
		//child.layout(true);
		endChange();
	}

}
